﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Data;
using System.IO;

namespace Agro.Library {
    public class StreamExcelResult : ActionResult {
        string _fileName;
        const string _fileExt = ".xls";//后缀
        MemoryStream _stream;
        
        public StreamExcelResult(MemoryStream stream, string fileName) {
            _fileName = fileName + _fileExt;
            _stream = stream;
        }

        public override void ExecuteResult(ControllerContext context) {
            context.HttpContext.Response.ContentType = "application/vnd.ms-excel";
            context.HttpContext.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", _fileName));
            context.HttpContext.Response.Clear();
            using (_stream) {
                _stream.WriteTo(context.HttpContext.Response.OutputStream);
            }
            context.HttpContext.Response.End();
        }
    }
}
